package cn.seocoo.platform.api.controller;

import cn.seocoo.platform.api.util.JSONResult;
import cn.seocoo.platform.api.util.PageBean;
import cn.seocoo.platform.base.domain.BaseBrand;
import cn.seocoo.platform.base.domain.BaseVehicleType;
import cn.seocoo.platform.base.service.IBaseBrandService;
import cn.seocoo.platform.base.service.IBaseVehicleTypeService;
import cn.seocoo.platform.car.dto.CarDto;
import cn.seocoo.platform.car.service.ICarInfoService;
import cn.seocoo.platform.common.CommonServiceImpl;
import cn.seocoo.platform.common.constant.ClassContants;
import cn.seocoo.platform.common.utils.AmountUtils;
import cn.seocoo.platform.common.utils.StringUtils;
import cn.seocoo.platform.contrast.domain.CarContrast;
import cn.seocoo.platform.contrast.service.ICarContrastService;
import cn.seocoo.platform.user.dto.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName: ApiContrastController
 * @description: 收藏/对比
 * @author: wujian
 * @Date: 2020-01-02 16:46
 **/
@RequestMapping("api/contrast")
@RestController
public class ApiContrastController {
    @Autowired
    private ICarContrastService iCarContrastService;



    @Autowired
    private CommonServiceImpl commonService;


    /**
     * @Author WuJian
     * @Description //加入收藏
     * @Date 17:23 2020/1/2
     * @Param [appUserId, carId]
     * @return cn.seocoo.platform.api.util.JSONResult
     **/
    @GetMapping("/saveContrast")
    public JSONResult saveContrast(Integer appUserId,Integer carId) {
        try {
            List<CarContrast> list =  iCarContrastService.selectCarContrastByUserAndCarId(appUserId,carId);
            if(StringUtils.isNotEmpty(list)){
                return JSONResult.ok(ClassContants.CONTRAST_MSG);
            }else{
                int row = iCarContrastService.save(appUserId, carId);
                if(row > 0 ){
                    return JSONResult.ok(ClassContants.CONTRAST_SUCCESS_MSG);
                }else{
                    return JSONResult.errorMsg(JSONResult.ERROR_MSG);
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.errorMsg(JSONResult.ERROR_MSG);
        }
    }


   /**
    * @Author WuJian
    * @Description //收藏列表
    * @Date 18:22 2020/1/2
    * @Param [appUserId]
    * @return cn.seocoo.platform.api.util.JSONResult
    **/
    @GetMapping("/getContrast")
    public JSONResult getContrast(Integer appUserId,Integer pageNum, Integer pageSize) {
        try {
            List<CarDto> carlist = iCarContrastService.getCarList(pageNum, pageSize,appUserId);
            PageBean<CarDto> carDtoPageInfo = new PageBean<>(carlist);
            List<CarDto> list = carDtoPageInfo.getList();
            commonService.getCarListForServcie(list);
            return JSONResult.ok(carDtoPageInfo);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return JSONResult.errorMsg(JSONResult.ERROR_MSG);
        }
    }


    /**
     * @Author WuJian
     * @Description //清空
     * @Date 17:23 2020/1/2
     * @Param [appUserId]
     * @return cn.seocoo.platform.api.util.JSONResult
     **/
    @GetMapping("/deleteContrast")
    public JSONResult deleteContrast(Integer appUserId) {
        try {
            int row = iCarContrastService.updateCarContrastForDel(appUserId);
            if(row > 0){
                return JSONResult.ok("清空成功");
            }
            return JSONResult.errorMsg(JSONResult.ERROR_MSG);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.errorMsg(JSONResult.ERROR_MSG);
        }
    }

    /**
     * @Author WuJian
     * @Description //取消收藏
     * @Date 17:23 2020/1/2
     * @Param [appUserId]
     * @return cn.seocoo.platform.api.util.JSONResult
     **/
    @GetMapping("/clearContrast")
    public JSONResult clearContrast(Integer id) {
        try {
            int row  = iCarContrastService.clearContrast(id);
            if(row > 0){
                return JSONResult.ok("取消成功");
            }
            return JSONResult.errorMsg(JSONResult.ERROR_MSG);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.errorMsg(JSONResult.ERROR_MSG);
        }
    }
}
